package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.mapper.targetSystem.IdeaMapper;
import com.xinchuang.entity.targetSystem.IdeaData;
import com.xinchuang.service.targetSystem.IdeaService;
import com.xinchuang.util.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IdeaServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-09-05 15:58
 * @Verion V1.0
 */
@Service
public class IdeaServiceImpl implements IdeaService {

    @Autowired
    private IdeaMapper ideaMapper;

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(0, 0);

    /**
     * 保留后面位数
     */
    private int SUFF = 4;

    @Override
    public Result selectIdeaTargetList(String systemId, boolean system, int pageNum, int pageSize) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = null;
        if (system) {
            list = ideaMapper.selectIdeaSystemTargetList(systemId);
        } else {
            list = ideaMapper.selectIdeaSystemsTargetList(systemId);
        }
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            for (Map<String, Object> map : pageInfo.getList()) {
                if (ObjectUtil.isNotNull(map.get("stage"))) {
                    map.put("sign", true);
                    map.put("stage", "第" + map.get("stage") + "阶段");
                } else {
                    map.put("sign", false);
                    map.put("stage", null);
                }
            }
        }
        resultMap.put("dataList", pageInfo.getList());
        resultMap.put("pageNum", pageInfo.getPageNum());
        resultMap.put("pageSize", pageInfo.getPageSize());
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("pageAll", pageInfo.getPages());
        return ResultUtil.success(resultMap);
    }

    @Override
    public Result selectIdeaList(String systemId) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Integer> list = ideaMapper.selectIdeaStage(systemId);
        if (CollectionUtil.isNotEmpty(list)) {
            Map<String, Object> map = null;
            StringBuffer sb = null;
            for (Integer integer : list) {
                map = new HashMap<>();
                sb = new StringBuffer();
                map.put("stage", integer);
                List<String> stageList = ideaMapper.selectIdeaStageList(systemId, integer);
                if (CollectionUtil.isNotEmpty(stageList)) {
                    for (String s : stageList) {
                        sb.append(s + ";");
                    }
                    map.put("targetList", sb.toString());

                } else {
                    map.put("targetList", "包含目标已被删除，请手动删除当前阶段");
                }
                resultList.add(map);
            }
        }
        return ResultUtil.success(resultList);
    }

    @Override
    public Result selectIdeaValueList(String systemId) {
        Map<String, Object> resultMap = new HashMap<>();
        Double aDouble = ideaMapper.selectIdeaValueSum(systemId);
        double total = 0D;
        if (ObjectUtil.isNull(aDouble)) {
            aDouble = 0D;
        } else {
            total = aDouble;
        }
        resultMap.put("total", new BigDecimal(total).setScale(SUFF, BigDecimal.ROUND_HALF_DOWN));
        List<Integer> list = ideaMapper.selectIdeaStage(systemId);
        List<String> xAxle = new ArrayList<>();
        List<Double> yAxle = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            double middle;
            xAxle.add("初始阶段");
            yAxle.add(new BigDecimal(total).setScale(SUFF, BigDecimal.ROUND_HALF_DOWN).doubleValue());
            for (int i = 0; i < list.size() + 1; i++) {
                middle = 0D;
                if (i == list.size()) {
                    xAxle.add("最后阶段");
                    yAxle.add(new BigDecimal(aDouble).setScale(SUFF, BigDecimal.ROUND_HALF_DOWN).doubleValue());
                } else {
                    xAxle.add("第" + list.get(i) + "阶段");
                    Double valueSum = ideaMapper.selectIdeaStageValueSum(systemId, list.get(i));
                    if (ObjectUtil.isNull(valueSum)) {
                        middle = aDouble;
                    } else {
                        middle = aDouble - valueSum;
                        aDouble -= valueSum;
                    }
                    yAxle.add(new BigDecimal(middle).setScale(SUFF, BigDecimal.ROUND_HALF_DOWN).doubleValue());
                }
            }
        }
        resultMap.put("lastTotal", new BigDecimal(aDouble).setScale(SUFF, BigDecimal.ROUND_HALF_DOWN));
        resultMap.put("xAxle", xAxle);
        resultMap.put("yAxle", yAxle);
        return ResultUtil.success(resultMap);
    }

    @Override
    public Result cleatIdea(String systemId) {
        if (ideaMapper.clearIdea(systemId) > 0) {
            return ResultUtil.success("清除成功", null);
        }
        return ResultUtil.error(500, "数据已被清空");
    }

    @Override
    public Result deleteIdeaStage(String systemId, Integer stage) {
        if (StringUtils.isBlank(systemId) || ObjectUtil.isNull(stage)) {
            return ResultUtil.error(500, "数据异常，请稍等重试");
        }
        List<Integer> list = ideaMapper.selectIdeaStage(systemId);
        if (ideaMapper.deleteIdeaStage(systemId, stage) > 0) {
            if (list.size() > 1) {
                for (int i = list.size(); i > stage; i--) {
                    ideaMapper.updateStageBySystemIdAndStage(systemId, list.size(), list.size() - 1);
                }
            }
            return ResultUtil.success("删除成功", null);
        }
        return ResultUtil.error(500, "数据已被删除");
    }

    @Override
    public synchronized Result addIdeaStage(String systemId, List<String> targetIdList) {
        if (StringUtils.isBlank(systemId) || CollectionUtil.isEmpty(targetIdList)) {
            return ResultUtil.error(500, "数据异常，请稍等重试");
        }
        int stage = 1;
        List<Integer> list = ideaMapper.selectIdeaStage(systemId);
        if (CollectionUtil.isNotEmpty(list)) {
            stage = list.size() + 1;
        }
        List<IdeaData> dataList = new ArrayList<>();
        IdeaData idea = null;
        for (String targetId : targetIdList) {
            idea = new IdeaData();
            idea.setId(snow.nextId() + "");
            idea.setSystemId(systemId);
            idea.setTargetId(targetId);
            idea.setStage(stage);
            dataList.add(idea);
        }
        ideaMapper.insertIdeaStage(dataList);
        return ResultUtil.success();
    }

}